<template>
  <el-row type="flex" justify="space-between" align="middle" style=" width:100%;height:40px">
    <el-col>
      <span>{{ data.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>{{ data.manager }}</el-col>
        <el-col>
          <el-dropdown @command="operateDepts">
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <el-dropdown-menu>
              <!-- 问题:下拉图片堆叠问题 -->
              <!-- 原因:标题名称都一样,node-key属性 -->
              <!-- 没有给node-key属性,name一旦重复导致冲突 -->
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <template v-if="!isRoot">
                <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                <el-dropdown-item command="del">删除部门</el-dropdown-item>
              </template>

            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'
export default {
  props: {
    data: {
      required: true,
      type: Object
    },
    isRoot: {
      type: Boolean,
      default: false// 是否在根节点使用
    }

  },
  methods: {
    operateDepts(type) {
      if (type === 'add') {
        // 添加子部门的操作
        // 告诉父组件 显示弹层
        this.$emit('addDeparts', this.data) // 为何传出treeNode 因为是添加子部门 需要当前部门的数据
      } else if (type === 'edit') {
        this.$emit('editDeparts', this.data)
      } else {
        this.$confirm('确定要删除该部门吗').then(() => {
          return delDepartments(this.data.id)
        }).then(() => {
          this.$message.success('删除部门成功')
          this.$emit('delDeparts')
        })
      }
    }
  }
}
</script>

<style>

</style>
